package org.xshaokuang.loginandemail.email;

import cn.nukkit.Player;
import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.event.Listener;

import java.util.List;

public class EmailOpenCommand extends Command implements Listener {
    private final EmailDataManager manager;

    public EmailOpenCommand(EmailDataManager manager) {
        super("xemailopen", "打开邮箱", "/xemailopen");
        this.manager = manager;
        this.setPermission("email.command.open");
    }

    @Override
    public boolean execute(CommandSender sender, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("§c只有玩家可以使用此命令");
            return true;
        }

        Player player = (Player) sender;

        if (args.length == 0) {
            openEmail(player, player.getName());
            return true;
        }

        if(args.length == 1 && player.isOp()) {
            openEmail(player, args[0]);
        }

        return true;
    }

    private void openEmail(Player player,String name) {
        List<EmailData> emails = manager.getPlayerEmails(name);
        EmailForm.showMainForm(player, emails);
    }
}
